package com.example.administrator.printoutindemo.adapter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dothantech.lpapi.IAtBitmap;
import com.dothantech.lpapi.LPAPI;
import com.dothantech.printer.IDzPrinter;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.bean.RukuSuccessBean;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.utils.SharedPreferencesHelper;
import com.xiaoxing.zhinenghuacangku.mvp.ui.utils.PrinterUtil;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;

/**
 * Created by noc on 2018/10/25.
 */

public class RuKuSuccessAdapter extends BaseAdapter {

    protected SharedPreferencesHelper mSharedPreferencesHelper;
    Context context;
    List<RukuSuccessBean> listm;
    LayoutInflater inflater;
    PrinterUtil mPrinterUtil;
    private IRuKuSuccessAdapter mIRuKuSuccessAdapter;
    private int mTouchItemPosition = 0;
    private int itemEditTextPosition = 0;

    public RuKuSuccessAdapter(Context context, List<RukuSuccessBean> listm, IRuKuSuccessAdapter iRuKuSuccessAdapter) {
        this.context = context;
        this.listm = listm;
        this.mIRuKuSuccessAdapter = iRuKuSuccessAdapter;
        this.inflater = LayoutInflater.from(context);
        mPrinterUtil = new PrinterUtil(context);
        IDzPrinter.Factory.getInstance().init(context, mPrinterUtil.mCallback);
        mSharedPreferencesHelper = SharedPreferencesHelper.getInstance(context);

    }

    public void shua() {
        if (listm == null)
            listm = new ArrayList<>();
        notifyDataSetInvalidated();
    }

    @Override
    public int getCount() {
        return listm.size();
    }

    @Override
    public Object getItem(int i) {
        return listm.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = getInflater().inflate(R.layout.rukusuccessitem, null);
            holder = new ViewHolder();
            holder.tv_xmmc = view.findViewById(R.id.tv_xmmc);
            holder.tv_gys = view.findViewById(R.id.tv_gys);
            holder.tv_ddh = view.findViewById(R.id.tv_ddh);
            holder.tv_wlmc = view.findViewById(R.id.tv_wlmc);
            holder.tv_shuliang = view.findViewById(R.id.tv_shuliang);
            holder.tv_rksj = view.findViewById(R.id.tv_rksj);
            holder.tv_cwmc = view.findViewById(R.id.tv_cwmc);
            holder.tv_dyewm = view.findViewById(R.id.tv_dyewm);
            holder.tv_cha_kan_zhao_pian = view.findViewById(R.id.tv_cha_kan_zhao_pian);
            holder.tv_bei_zhu = view.findViewById(R.id.tv_bei_zhu);
            holder.tv_xiu_gai_bei_zhu = view.findViewById(R.id.tv_xiu_gai_bei_zhu);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        //赋值
        if (listm.get(i).getProjectname() != null) {
            holder.tv_xmmc.setText(listm.get(i).getProjectname());
        }
        if (listm.get(i).getSupplier() != null) {
            holder.tv_gys.setText(listm.get(i).getSupplier());
        }
        if (listm.get(i).getPurchaseid() != null) {
            holder.tv_ddh.setText(listm.get(i).getPurchaseid());
        }
        if (listm.get(i).getMaterial() != null) {
            holder.tv_wlmc.setText(listm.get(i).getMaterial());
        }
        if (listm.get(i).getNums() != null) {
            holder.tv_shuliang.setText(listm.get(i).getNums());
        }
        if (listm.get(i).getInstoretime() != null) {
            holder.tv_rksj.setText(listm.get(i).getInstoretime());
        }
        if (listm.get(i).getSpname() != null) {
            holder.tv_cwmc.setText(listm.get(i).getSpname());
        }

        holder.tv_dyewm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mSharedPreferencesHelper.getString(BaseConstants.ER_WEI_MA_TYPE).equals("1")) {
                    printQRcode(String.valueOf(listm.get(i).getMid()), listm.get(i).getPurchaseid(), listm.get(i).getMaterial(), listm.get(i).getNums(), listm.get(i).getInstoretime(), listm.get(i).getProid());
                } else {
//                    printBigQRcode(String.valueOf(listm.get(i).getMid()), listm.get(i).getPurchaseid(), listm.get(i).getMaterial(), listm.get(i).getNums(), listm.get(i).getInstoretime(), listm.get(i).getProid());
                    printBigQRcodeOriention(String.valueOf(listm.get(i).getMid()), listm.get(i).getPurchaseid(), listm.get(i).getMaterial(), listm.get(i).getNums(), listm.get(i).getInstoretime(), listm.get(i).getProid());
                }
                Log.e("生成二维码的mid=======", listm.get(i).getMid() + "");

            }
        });
        holder.tv_cha_kan_zhao_pian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("item", listm.get(i));
                    Utils.navigation(context, RouterHub.ACTIVITY_RU_KU_CHENG_GONG_JI_LU_WU_LIAO_TU_PIAN, bundle);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        if (listm.get(i).getRemark() != null) {
            holder.tv_bei_zhu.setText(listm.get(i).getRemark());
        }

        ViewHolder finalHolder = holder;
        holder.tv_xiu_gai_bei_zhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mIRuKuSuccessAdapter.modifyRenark(listm.get(i),  finalHolder.tv_bei_zhu.getText().toString().trim());
            }
        });


        return view;
    }

    public LayoutInflater getInflater() {
        return inflater;
    }

    public void setInflater(LayoutInflater inflater) {
        this.inflater = inflater;
    }

    /**
     * 小的纸张 50x20 的
     *
     * @param content
     * @param orderNo
     * @param name
     * @param num
     * @param time
     * @param proid
     */
    private void printQRcode(String content, String orderNo, String name, String num, String time, String proid) {
        if (mPrinterUtil.selectPrinterCheck()) {

            // 得到 IDzPrinter 对象
            IDzPrinter printer = IDzPrinter.Factory.getInstance();
            // 连接配对的第一个打印机对象
            printer.connect(IDzPrinter.Factory.getFirstPrinter());
            // 创建 IAtBitmap 绘图对象
            IAtBitmap api = IAtBitmap.Factory.createInstance();
            // 开始绘图任务，传入参数(页面宽度, 页面高度)
            api.startJob(48 * 100, 20 * 100);//48, 50
            // 开始一个页面的绘制，绘制二维码
            api.draw2DQRCode(content, 0 * 100, 1 * 100, 15 * 100);//  9, 10, 20,30
            api.drawText("订单号:" + orderNo, 16 * 100, 0 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
            if (name.length() > 15) {
                if (name.substring(15).length() > 20) {
                    api.drawText("核对序号:" + content, 16 * 100, 3 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("项目编号:" + proid, 16 * 100, 6 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("物料名称:" + name.substring(0, 15), 16 * 100, 96 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("" + name.substring(15, 35), 16 * 100, 12 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("" + name.substring(35), 16 * 100, 15 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
//                api.drawText("数量:" + num, 16 * 100, 12 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR);
                    api.drawText("入库时间:" + time, 16 * 100, 18 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                } else {
                    api.drawText("核对序号:" + content, 16 * 100, 3 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("项目编号:" + proid, 16 * 100, 6 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("物料名称:" + name.substring(0, 15), 16 * 100, 9 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("" + name.substring(15), 16 * 100, 12 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
//                api.drawText("数量:" + num, 16 * 100, 9 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR);
                    api.drawText("入库时间:" + time, 16 * 100, 15 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                }

            } else {
                api.drawText("核对序号:" + content, 16 * 100, 3 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("项目编号:" + proid, 16 * 100, 6 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("物料名称:" + name, 16 * 100, 9 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
//            api.drawText("数量:" + num, 16 * 100, 6 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR);
                api.drawText("入库时间:" + time, 16 * 100, 12 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
            }

            // 结束绘图任务
            api.endJob();
            // 打印
            Bundle param = new Bundle();
//            param.putInt(IDzPrinter.PrintParamName.PRINT_DENSITY, 1);
            printer.print(api, null);
        }
    }
    private  void printBigQRcodeOriention(String content, String orderNo, String name, String num, String time, String proid) {

        // 得到 IDzPrinter 对象
        IDzPrinter printer = IDzPrinter.Factory.getInstance();
        // 连接配对的第一个打印机对象
        printer.connect(IDzPrinter.Factory.getFirstPrinter());
        // 创建 IAtBitmap 绘图对象
        IAtBitmap api = IAtBitmap.Factory.createInstance();
        // 开始绘图任务，传入参数(页面宽度, 页面高度)
        api.setItemHorizontalAlignment(LPAPI.ItemAlignment.LEFT);
        api.setItemOrientation(180);

        api.startJob(50 * 100, 50 * 100);//48, 50
//        api.setItemOrientation(180);

        // 开始一个页面的绘制，绘制二维码
        if (name.length() > 19) {
            if (name.substring(19).length() > 23) {
                api.drawText("入库时间:" + time, 46 * 100, 4 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("" + name.substring(42), 46 * 100, 7 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("" + name.substring(19, 42), 46 * 100, 10 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("物料名称:" + name.substring(0, 19), 46 * 100, 13 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("项目编号:" + proid, 46 * 100, 16 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("核对序号:" + content, 46 * 100, 19 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
//                api.drawText("数量:" + num, 16 * 100, 12 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR);
                api.drawText("订单号:" + orderNo, 46 * 100, 22 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.draw2DQRCode(content, 14 * 100, 23 * 100, 17 * 100);//  9, 10, 20,30
            } else {
                api.drawText("入库时间:" + time, 46 * 100, 4 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("" + name.substring(19), 46 * 100, 7 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("物料名称:" + name.substring(0, 19), 46 * 100, 10 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("项目编号:" + proid, 46 * 100, 13 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("核对序号:" + content, 46 * 100, 16 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
//                api.drawText("数量:" + num, 16 * 100, 9 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR);
                api.drawText("订单号:" + orderNo, 46 * 100, 19 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.draw2DQRCode(content, 14 * 100, 20 * 100, 20 * 100);//  9, 10, 20,30
            }

        } else {
            api.drawText("入库时间:" + time, 46 * 100, 4 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
            api.drawText("物料名称:" + name, 46 * 100, 7 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
            api.drawText("项目编号:" + proid, 46 * 100, 10 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
            api.drawText("核对序号:" + content, 46 * 100, 13 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
//            api.drawText("数量:" + num, 16 * 100, 6 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR);
            api.drawText("订单号:" + orderNo, 46 * 100, 16 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
            api.draw2DQRCode(content, 14 * 100, 17 * 100, 20 * 100);//  9, 10, 20,30
        }

        // 结束绘图任务
        api.endJob();
        // 打印
        printer.print(api, null);

    }

    /**
     * 大的纸张 50x50的
     *
     * @param content
     * @param orderNo
     * @param name
     * @param num
     * @param time
     * @param proid
     */
    private void printBigQRcode(String content, String orderNo, String name, String num, String time, String proid) {
        if (mPrinterUtil.selectPrinterCheck()) {

            // 得到 IDzPrinter 对象
            IDzPrinter printer = IDzPrinter.Factory.getInstance();
            // 连接配对的第一个打印机对象
            printer.connect(IDzPrinter.Factory.getFirstPrinter());
            // 创建 IAtBitmap 绘图对象
            IAtBitmap api = IAtBitmap.Factory.createInstance();
            // 开始绘图任务，传入参数(页面宽度, 页面高度)
            api.startJob(48 * 100, 50 * 100);//48, 50
            // 开始一个页面的绘制，绘制二维码
            api.draw2DQRCode(content, 10 * 100, 5 * 100, 25 * 100);//  9, 10, 20,30
            api.drawText("订单号:" + orderNo, 0 * 100, 32 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
            if (name.length() > 25) {
                if (name.substring(25).length() > 20) {
                    api.drawText("核对序号:" + content, 0 * 100, 35 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("项目编号:" + proid, 0 * 100, 38 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("物料名称:" + name.substring(0, 15), 0 * 100, 41 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("" + name.substring(15, 35), 0 * 100, 44 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("" + name.substring(35), 0 * 100, 47 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
//                api.drawText("数量:" + num, 16 * 100, 12 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR);
                    api.drawText("入库时间:" + time, 0 * 100, 50 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                } else {
                    api.drawText("核对序号:" + content, 0 * 100, 35 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("项目编号:" + proid, 0 * 100, 38 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("物料名称:" + name.substring(0, 15), 0 * 100, 41 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                    api.drawText("" + name.substring(15), 0 * 100, 44 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
//                api.drawText("数量:" + num, 16 * 100, 9 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR);
                    api.drawText("入库时间:" + time, 0 * 100, 47 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                }

            } else {
                api.drawText("核对序号:" + content, 0 * 100, 35 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("项目编号:" + proid, 0 * 100, 38 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
                api.drawText("物料名称:" + name, 0 * 100, 41 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
//            api.drawText("数量:" + num, 16 * 100, 6 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR);
                api.drawText("入库时间:" + time, 0 * 100, 44 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
            }

            // 结束绘图任务
            api.endJob();
            Bundle param = new Bundle();
//            param.putInt(IDzPrinter.PrintParamName.PRINT_DENSITY, 1);
            // 打印
            printer.print(api, null);
        }
    }

    public void addAll(List<RukuSuccessBean> sdc) {
        listm.addAll(sdc);
        notifyDataSetChanged();
    }

    public void clearData() {
        listm.clear();
    }

    public interface IRuKuSuccessAdapter {
        void modifyRenark(RukuSuccessBean rukuSuccessBean, String remark);
    }

    class ViewHolder {
        TextView tv_xmmc, tv_gys, tv_ddh, tv_wlmc, tv_shuliang, tv_rksj, tv_cwmc, tv_dyewm, tv_cha_kan_zhao_pian, tv_xiu_gai_bei_zhu;
        TextView tv_bei_zhu;
    }


}
